window.onload = function () {
    //阻止默认行为
    document.querySelector('.box').addEventListener('touchmove', function (e) {
        e.preventDefault();
    });

    document.body.addEventListener('dblclick', function (e) {
        e.preventDefault();
    });

    document.documentElement.addEventListener('dblclick', function (e) {
        e.preventDefault();
    });

    //获取元素
    let btn = document.querySelector('#qw');
    let show = document.querySelector('.q');
    let box1 = document.querySelector('.box1');
    let flag = true;
    let q = 0;
    let num = 0;
    //自动点击

    //点击按钮，开始运行

    btn.addEventListener('click', function () {
        let random = parseInt(Math.random() * 9) + 32;
        if (flag) {
                flag = false;
                show.innerHTML = '好运连连~~';
                let span = document.querySelectorAll('span');
                let timer1 = setInterval(function () {
                    fn();
                }, 100)

                function fn() {
                    var c = span[q].dataset.index - 1;
                    for (var k = 0; k < span.length; k++) {
                        span[k].style.backgroundColor = '';
                    }
                    span[c].style.backgroundColor = 'rgb(195, 108, 31)'
                    q++;
                    if (q == 8) {
                        q = 0;
                    }
                    num++;
                    //变化时间  
                    if (num > 24 && num <= 28) {
                        clearInterval(timer1)
                        timer1 = setInterval(function () {
                            fn();
                        }, 200)
                    }

                    if (num > 28 && num <= 32) {
                        clearInterval(timer1)
                        timer1 = setInterval(function () {
                            fn();
                        }, 400)
                    }

                    if (num > 32) {
                        clearInterval(timer1)
                        timer1 = setInterval(function () {
                            fn();
                        }, 500)
                    }

                    if (num >= random) {
                        clearInterval(timer1);
                        num = 0;
                        if (span[c].innerHTML == '获奖') {
                            show.innerHTML = '恭喜你' + span[c].innerHTML;
                        } else if (span[c].innerHTML == '永远年轻') {
                            show.innerHTML = '祝愿你' + span[c].innerHTML;
                        } else if (span[c].innerHTML == '快乐永久') {
                            show.innerHTML = '祝愿你' + span[c].innerHTML;
                        }
                        else {
                            show.innerHTML = '恭喜你获得' + span[c].innerHTML + '称号';
                        }
                        flag = true;
                    }
                }}
    })
}

